package com.utils;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpToolUtils {

    public static String sendGet(String url, String token)throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Authorization", "Bearer " + token);
        CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
        return getObjectObjectMap(httpResponse);
    }

    public static String sendPost(String url, String params, String token) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Authorization", "Bearer " + token);
        StringEntity stringEntity = new StringEntity(params);
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
        return getObjectObjectMap(httpResponse);
    }
    
    public static String sendPost(String url, String params) throws Exception {
    	CloseableHttpClient httpclient = HttpClients.createDefault();
    	HttpPost httpPost = new HttpPost(url);
    	StringEntity stringEntity = new StringEntity(params);
    	stringEntity.setContentType("application/json");
    	httpPost.setEntity(stringEntity);
    	CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
    	return getObjectObjectMap(httpResponse);
    }

    private static String getObjectObjectMap(CloseableHttpResponse httpResponse) throws IOException {
        String result = null;
        try {
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity, "utf-8");
            EntityUtils.consume(httpEntity);
        } finally {
            try {
                if (httpResponse != null) {
                    httpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
	public static String doGetXml(String url, String xmlDate, String apiToken) {
		String ret;
		try{
			HttpGet httpGet = new HttpGet();
			httpGet.setHeader("Authorization", "basic "+apiToken);
			httpGet.setHeader("Content-Type", "text/xml;charset=utf-8");
			HttpUtilFinal http = HttpUtilFinal.getInstance();
			ret = http.doGetXml(httpGet, url);
		} catch (Exception e) {
			ret = "";
		}
		return ret;
	}
	
	public static String doGetXml(String url) {
		String ret;
		try{
			HttpGet httpGet = new HttpGet();
			httpGet.setHeader("Content-Type", "text/xml;charset=utf-8");
			HttpUtilFinal http = HttpUtilFinal.getInstance();
			ret = http.doGetXml(httpGet, url);
		} catch (Exception e) {
			ret = "";
		}
		return ret;
	}
	
	/**
	 * 执行一个HTTP POST请求,参数为XML类型的字符串
	 * @param url		地址
	 * @param xmlData	xml格式
	 * @param keys		物流apiToken
	 * @return String
	 */
	public static String doPostXml(String url, String xmlData, Map<String, String> keys) {
		String ret;
		try{
			HttpPost httpPost = new HttpPost();
			for (Map.Entry<String, String> entry : keys.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
			if(!keys.containsKey("Content-Type")) {
				httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
			}
			StringEntity strEntity = new StringEntity(xmlData, "UTF-8");
			httpPost.setEntity(strEntity);

			HttpUtilFinal http = HttpUtilFinal.getInstance();
			ret = http.doPostXml(httpPost, url);
			if(ret == null){
				ret = "";
        	}
		} catch (Exception e) {
			ret = "";
		}
		return ret;
	}
}
